package com.jfcloud.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapUtil {
	public static Double getDouble(String key, Map requestMap) {
		String v = String.valueOf( requestMap.get(key));
		if("null".equals(v)){
			return null;
		}
		if (StringUtil.isNotEmpty(v)) {
			return Double.valueOf(v);
		} else {
			return null;
		}
	}
	
	public static String getString(String string, Map<String, Object> form) {
		String v =  String.valueOf(  form.get(string) );
		if("null".equals(v)){
			return null;
		}
		return v;
	}
	
	public static Long getLong(String key, Map requestMap) {
		String v = String.valueOf( requestMap.get(key));
		if("null".equals(v)){
			return null;
		}
		if (StringUtil.isNotEmpty(v)) {
			return Long.valueOf(v);
		} else {
			return null;
		}
	}
	
	public static Integer getInteger(String key, Map requestMap) {
		String v = String.valueOf( requestMap.get(key));
		if("null".equals(v)){
			return null;
		}
		if (StringUtil.isNotEmpty(v)) {
			return Integer.valueOf(v);
		} else {
			return null;
		}
	}
	
	
	/**
	 * 两个列表合并
	 * @param subLists 子表
	 * @param subField 子表和主表匹配的字段名 
	 * @param mainLists 主表
	 * @param mainField 子表和主表匹配的字段名 
	 * @param subListName 添加后主表中的子表字段名
	 */
	public static void subIntoMain(List<Map<String, Object>> subLists, String subField, List<Map<String, Object>> mainLists,
			String mainField,String subListName) {
		for(Map m:mainLists){
			String mf= String.valueOf(m.get(mainField)==null?"":m.get(mainField));
			for(Map mm:subLists){
				String sf = String.valueOf(mm.get(subField)==null?"":mm.get(subField));
				if(StringUtil.isNotEmpty(sf)&&StringUtil.isNotEmpty(mf)
						&&sf.equals(mf)){
					List<Map<String,Object>> subList = (List<Map<String, Object>>) m.get(subListName);
					if(subList==null){
						subList =  new ArrayList<Map<String,Object>>();
						m.put(subListName, subList);
					}
					subList.add(mm);
				}
			}
		}
	}
}
